﻿using System.Collections;
using System.Collections.Generic;
using Utility.Events;
using Utility.DataStructure;

namespace Utility.TimerSystem
{
	public interface ITimer
	{
		float DeltaTime { get; }
		float Time { get; }
	}

	public class Timer : ITimer
	{
		public Event<Timer> E_OnUpdate { get; } = new Event<Timer> ();
		public TimerEventSchedule Schedule { get; }

		public Timer ()
		{
			Schedule = new TimerEventSchedule (this);
		}

		float _timescale = 1;
		public float TimeScale
		{
			get => _timescale;
			set => _timescale = System.Math.Max (0, value);
		}

		public float DeltaTime { get; private set; }
		public float Time { get; private set; }
		public float Time_Last => Time - DeltaTime;


		public void Update (float dt)
		{
			DeltaTime = dt * TimeScale;
			Time += DeltaTime;
			//events
			E_OnUpdate.Invoke (this);
			Schedule.UpdateEvents ();
		}

		public void Reset ()
		{
			DeltaTime = 0;
			Time = 0;
			TimeScale = 1;

			E_OnUpdate.Clear ();
			Schedule.Clear ();
		}
	}
}